package com.cy.sys.service.impl;

import com.cy.pj.common.pojo.Node;
import com.cy.sys.dao.SysMenuDao;
import com.cy.sys.pojo.SysMenu;
import com.cy.sys.service.SysMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 菜单业务对象
 */

@Transactional(readOnly = false,
        rollbackFor = Throwable.class,
        isolation = Isolation.READ_COMMITTED) //回滚
@Service
public class SysMenuServiceImpl implements SysMenuService {
    @Autowired
    private SysMenuDao sysMenuDao;

    @Transactional(readOnly = true)//该属性用于设置当前事务是否为只读事务
    @Override
    public List<SysMenu> findMenus() {
        //方式1 (重点)
        return sysMenuDao.selectMenus();
        //方式2 (拓展)
        //List<SysMenu> menus=sysMenuDao.selectMenus();
        //查找所有菜单信息:select * from sys_menus
        //在业务对数据进行封装(一级菜单,二级菜单,三级菜单),递归算法
        //menus.stream().filter().map().collect()
    }

    @Transactional(readOnly = true)
    @Override
    public List<Node> findMenuTreeNodes() {
        return sysMenuDao.selectMenuTreeNodes();
    }

    @Override
    public int saveMenu(SysMenu menu) {
        return sysMenuDao.insertMenu(menu);
    }

    @Transactional(readOnly = true)
    @Override
    public SysMenu findById(Integer id) {
        return sysMenuDao.selectById(id);
    }

    @Transactional(readOnly = false)//不出现并发
    @Override
    public int updateMenu(SysMenu menu) {
        return sysMenuDao.updateMenu(menu);
    }
}
